package cn.com.git.example.common.auth;

import cn.com.git.example.common.util.object.DateUtil;

import java.io.Serializable;
import java.util.Date;

/**
 * 记录登录过程
 */
public class LoginProcessDTO implements Serializable {

    /**
     * 登录等待毫秒数
     */
    private static final Integer LOGIN_WAIT_SECONDS = 60 ;
    /**
     * 最大登录失败次数
     */
    private static final Integer MAX_TRY_TIMES = 3;
    //session id
    private String sessionId ;
    //账号
    private String account;
    //上一次登录失败时间
    private Date lastLoginFailTime ;
    //登录失败次数
    private Integer failTimes ;
    //验证码
    private String verifyCode;

    public LoginProcessDTO() {
    }

    public LoginProcessDTO(String sessionId) {
        this.sessionId = sessionId;
    }

    public String getSessionId() {
        return sessionId;
    }

    public void setSessionId(String sessionId) {
        this.sessionId = sessionId;
    }

    public String getAccount() {
        return account;
    }

    public void setAccount(String account) {
        this.account = account;
    }

    public Date getLastLoginFailTime() {
        return lastLoginFailTime;
    }

    public void setLastLoginFailTime(Date lastLoginFailTime) {
        this.lastLoginFailTime = lastLoginFailTime;
    }

    public Integer getFailTimes() {
        return failTimes;
    }

    public void setFailTimes(Integer failTimes) {
        this.failTimes = failTimes;
    }

    public String getVerifyCode() {
        return verifyCode;
    }

    public void setVerifyCode(String verifyCode) {
        this.verifyCode = verifyCode;
    }

    /**
     * 失败次数递增
     * @return 返回递增后的次数
     */
    public Integer failTimesIncrease(){
        if(failTimes == null){
            failTimes = 1 ;
        }else{
            failTimes += 1 ;
        }
        return failTimes ;
    }
    /**
     * 是否开启验证码
     * @return true是，false否
     */
    public boolean verifyCodeEnable() {
        return this.failTimes>0;
    }
    /**
     * 页面展示等待秒数
     * @return
     */
    public Integer waitSeconds() {
        //如果未开启验证码登录，则不进行登录等待限制
        if(!this.verifyCodeEnable()){
            return 0;
        }
        //如果当前失败次数小于或者等于最大尝试次数，则不需要登录等待
        if(this.MAX_TRY_TIMES>=this.failTimes){
            return 0 ;
        }
        //在验证码开启且失败次数超限的情况下，根据最后失败时间返回等待秒数
        //计算当前时间与上一次登录失败时间之间的秒数
        int diffSecond = DateUtil.DateDiff(new Date(), this.lastLoginFailTime,"s");
        //计算等待时间
        int realWaitSecond = LOGIN_WAIT_SECONDS - diffSecond ;
        //返回的等待时间不能小于0
        return realWaitSecond<0?0:realWaitSecond;
    }
}
